package com.dy.controller;

import com.dy.domain.Gzlb;
import com.dy.domain.Living;
import com.dy.domain.Xinxi;
import com.dy.service.GuanzlbService;
import com.dy.service.LivingService;
import com.kaoshi.common.config.Result;
import net.polyv.live.v1.util.LiveSignUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.dy.util.HttpUtil;


@RequestMapping("/live/gz/")
@RestController
public class GuanController {
    @Autowired
    private LivingService livingService;
    @Autowired
    private GuanzlbService gzlbService;

    @RequestMapping("findAll")
    public List<Living> findAll(){
        List<Living> livingList=livingService.findAll();
        return livingList;
    }

    private static final Logger log = LoggerFactory.getLogger(GuanController.class);
    /**
     * 查询授权和连麦的token
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    @PostMapping("lts/{cid}")
    public Result testGetChatToken(@PathVariable Integer cid) throws IOException, NoSuchAlgorithmException {
        Xinxi xinxi=new Xinxi();
        //公共参数,填写自己的实际参数
        String appId ="h25m96k5li";
        String appSecret ="fff8cbb139794d6d8edf630bfcc76d66";
        String userId =""+cid;
        String timestamp = String.valueOf(System.currentTimeMillis());
        //业务参数
        String url = "http://api.polyv.net/live/v3/channel/common/get-chat-token";
        String channelId = "5382849";
        String role = "viewer";
        String origin = "web";

        Map<String, String> requestMap = new HashMap<>();
        requestMap.put("appId", appId);
        requestMap.put("timestamp", timestamp);
        requestMap.put("channelId", channelId);
        requestMap.put("userId", userId);
        requestMap.put("role", role);
        requestMap.put("origin", origin);
        requestMap.put("sign", LiveSignUtil.getSign(requestMap, appSecret));
        String response = HttpUtil.postFormBody(url, requestMap);
//        log.info("测试查询授权和连麦的token，返回值：{}", response);
        String a=response;
//        System.out.println(a);
        a=StringUtils.substringAfter(a, ":");
        a=StringUtils.substringAfter(a, ":");
        a=StringUtils.substringAfter(a, ":");
        a=StringUtils.substringAfter(a, ":");
        a=StringUtils.substringAfter(a, ":");
        a=StringUtils.substringAfter(a, ":");
        a=StringUtils.substringAfter(a, ":");
        a=StringUtils.substringAfter(a, "\"");
        xinxi.setA1(StringUtils.substringBefore(a, "\""));
        a=StringUtils.substringAfter(a, "token");
        a=StringUtils.substringAfter(a, "\"");
        a=StringUtils.substringAfter(a, "\"");
        System.out.println(a);
        a=StringUtils.substringBefore(a, "\"");
        xinxi.setA2(a);
        System.out.println(a);
//        System.out.println(a);
        System.out.println(xinxi.getA1());
        System.out.println(xinxi.getA2());
        return Result.ok(xinxi);
    }

    @RequestMapping("/gzlb/{lid}/{uid}")
    public Result gzlb(@PathVariable Integer lid,@PathVariable Integer uid){
        Gzlb gzlb=new Gzlb();
        gzlb.setLid(lid);
        gzlb.setUid(uid);
        Integer a= gzlbService.add(gzlb);
        return Result.ok(a);
    }

    @RequestMapping("/gzlb/{lid}")
    public Result zbgz(@PathVariable Integer lid){
        List<Gzlb> list=gzlbService.findzbgz(lid);
        return Result.ok(list);
    }


}
